home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / dragon / dragon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  17.7 KB  |  953 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*  9/6/88 David B. Ligon
  18.  
  19.    Modified dragon to include facilities for 8 bit machine.
  20.  
  21.    Additional variables and defines are below.  Additional routines may be
  22.    found at the end.
  23.  
  24.    Changed default bit map to a geometric progression so that more of the
  25.    interior of the dragon would be visible.
  26.  
  27.    Added code to save and restore the existing color map.
  28.  
  29.  
  30.    6/29/89 David Ligon
  31.    Converted to use RGB index into lookup table to get around the
  32.    ever-changing, gluttonous window system usage of the color map
  33.  
  34.     This has a number of impications: 
  35.  
  36.    1) The dragon must be redrawn when a color change option is picked.
  37.  
  38.    2) Interp becomes useless in building a continuous ramp ramp.
  39.  
  40.    3) Color ramp must be reset when picking color option, and when initializing
  41.    the program.
  42.  
  43.    Future enhancements should include:
  44.  
  45.    1) Since we build a software ramp for eightbit anyway, we could probably
  46.    use a patterned dithering as in ipaste;
  47.  
  48.    1) Would be nice to have a crosshair to pick first point.
  49.  
  50.    2) Redraw screen should be converted to use lrectwrite.
  51.  
  52.    3) There is no real reason why we shouldn't allow resizing the window - this
  53.    should be implemented.
  54.  */
  55.  
  56. #include <stdio.h>
  57. #include <gl.h>
  58. #include <math.h>
  59. #include <device.h>
  60. #include <gl/image.h>
  61. #include <time.h>
  62.  
  63. #define COF 1024
  64. #define EBP_COF 56
  65.  
  66. int x=0, y=0;
  67.  
  68. double a, b, c, d;
  69. double spl, spr, spb, spt;  /* space left right bottom top */
  70. double inq, inr;
  71.  
  72. long orx, ory, sizex, sizey;    /* window origin and size */
  73.  
  74. int depth = 256;
  75.  
  76. int res = 0;
  77. #define LOW 0
  78. #define HI 1
  79.  
  80.  
  81. int mode=0;
  82. #define MAP 0
  83. #define DRAGON 1
  84.  
  85. short mx, my;
  86. short dev,val;
  87.  
  88. short *save;
  89.  
  90. int menu, color_menu;
  91. long XMAX, YMAX;
  92.  
  93. /* new for Eclipse 8 bit machine */
  94. Colorindex *ebp_maparray;
  95. int eightbit = 0;
  96.  
  97.  
  98. abs(i) {
  99.     if (i<0) return(-i); else return(i);
  100. }
  101.  
  102. sgn(i) {
  103.     if (i>0) return(1);
  104.     if (i<0) return(-1);
  105.     return(0);
  106. }
  107.  
  108. main (argc, argv)
  109. int    argc;
  110. char    *argv[];
  111. {
  112.  
  113.     if (argc == 3) {
  114.     sscanf(argv[1], "%lf", &inq);
  115.     sscanf(argv[2], "%lf", &inr);
  116.     } else {
  117.     inq = inr = 0.0;
  118.     }
  119.  
  120.     {
  121.     char *t, *strrchr();
  122.     winopen((t=strrchr(argv[0], '/')) != NULL ? t+1 : argv[0]);
  123.     }
  124.  
  125.     winattach();
  126.  
  127.     singlebuffer();
  128.     gconfig();
  129.     shademodel(FLAT);
  130.  
  131.     /* new for Eclipse 8 bit machine */
  132.     if (getgdesc(GD_BITS_NORM_SNG_RED) < 8) {
  133.         eightbit = 1;
  134.        ebp_maparray = (Colorindex *)malloc(1024 * sizeof(Colorindex));
  135.     }
  136.      XMAX = getgdesc(GD_XPMAX);
  137.      YMAX = getgdesc(GD_YPMAX);
  138.  
  139.     save_colormap();
  140.  
  141.     color(BLACK);
  142.     clear();
  143.  
  144.     qdevice(ESCKEY);
  145.     qdevice(RIGHTMOUSE);
  146.     qdevice(MIDDLEMOUSE);
  147.     qdevice(LEFTMOUSE);
  148.     qdevice(WINQUIT);
  149.     qdevice(INPUTCHANGE);
  150.     qdevice(WINSHUT);
  151.  
  152.     getorigin(&orx, &ory);
  153.     getsize(&sizex, &sizey);
  154.  
  155.     prefsize(sizex, sizey);
  156.     winconstraints();
  157.  
  158.     save = (short *)malloc(sizey * sizex * sizeof(short));
  159.  
  160.     make_colors(0);
  161.  
  162.     make_menus();
  163.     init_values();
  164.  
  165.     while (TRUE) {
  166.  
  167.     while (qtest()) {
  168.  
  169.         /* switch(dev=qread(&val)) { */
  170.         dev = qread(&val);
  171.         switch (dev) {
  172.  
  173.         case ESCKEY:
  174.             if (val)    /* ESC key DOWN, ignore */
  175.             break;
  176.  
  177.         case WINQUIT:
  178.             if (eightbit) {
  179.             drawmode(PUPDRAW);
  180.             color(0);
  181.             clear();
  182.             drawmode(NORMALDRAW);
  183.             }
  184.             restore_colormap();
  185.             exit(0);
  186.             break;
  187.  
  188.         case REDRAW:
  189.             reshapeviewport();
  190.             getorigin(&orx, &ory);
  191.             getsize(&sizex, &sizey);
  192.             redraw_screen();
  193.             break;
  194.  
  195.         case LEFTMOUSE:
  196.             if (val) make_new_space();
  197.             break;
  198.  
  199.         case MIDDLEMOUSE:
  200.             if (mode == MAP && val) {
  201.             select_point();
  202.             mode=DRAGON;
  203.             init_values();
  204.             }
  205.             break;
  206.  
  207.         case RIGHTMOUSE:
  208.             if (val) {
  209.             switch (dopup(menu)) {
  210.  
  211.                 case 1:
  212.                 mode=MAP;
  213.                 init_values();
  214.                 break;
  215.  
  216.                 case 2:
  217.                 save_rgb_picture();
  218.                 break;
  219.  
  220.                 case 3:
  221.                 save_picture();
  222.                 break;
  223.  
  224.                 case 4:
  225.                 make_colors(dopup(color_menu));
  226.                 if (eightbit)
  227.                     redraw_screen();
  228.                 break;
  229.  
  230.                 case 5:
  231.                 if (eightbit) {
  232.                     drawmode(PUPDRAW);
  233.                     color(0);
  234.                     clear();
  235.                     drawmode(NORMALDRAW);
  236.                 }
  237.                 restore_colormap();
  238.                 exit(0);
  239.             }
  240.             }
  241.             break;
  242.         }
  243.     }
  244.  
  245.     switch (mode) {
  246.  
  247.         case MAP:
  248.         do_map();
  249.         break;
  250.  
  251.         case DRAGON:
  252.         do_dragon();
  253.         break;
  254.         
  255.     }
  256.     }
  257. }
  258.  
  259.  
  260. init_values() {
  261.  
  262.     int i, j;
  263.  
  264.     for (i=0; i<sizex; i++) for (j=0; j<sizey; j++) save[i*sizey + j] = 0;
  265.  
  266.     x=0; y=0;
  267.  
  268.     if (mode==MAP) {
  269.  
  270.     spr = 2.2;
  271.     spl = -0.8;
  272.     spt = 1.5 * (float)sizey/(float)sizex;
  273.     spb = -1.5 * (float)sizey/(float)sizex;
  274.  
  275.     } else {
  276.  
  277.     spl = -2.0;
  278.     spr = 2.0;
  279.     spt = 2.0 * (float)sizey/(float)sizex;
  280.     spb = -2.0 * (float)sizey/(float)sizex;
  281.     }
  282.  
  283.     res = LOW;
  284. }
  285.  
  286. save_rgb_picture() {
  287.     
  288.     if (saveRGBscreen("dragon.rgb",  sizex, sizey)) {
  289.  
  290.     fprintf(stderr, "dragon: can't save image\n");
  291.     }
  292.  
  293. }
  294.  
  295.  
  296. save_picture() {
  297.  
  298.     if (savescreen("dragon.img",  sizex, sizey)) {
  299.  
  300.     fprintf(stderr, "dragon: can't save image\n");
  301.     }
  302.  
  303. }
  304.  
  305. redraw_screen() {
  306.  
  307.     int i, j;
  308.  
  309.     if (eightbit) {
  310.     for (i=0; i<sizex; i+=3) for (j=0; j<sizey; j+=3) {
  311.         color(ebp_maparray[save[i * sizey + j]]);
  312.         rectfi(i,j,i + 2,j + 2);
  313.     }
  314.     } else {
  315.     for (i=0; i<sizex; i+=3) for (j=0; j<sizey; j+=3) {
  316.         color(save[i*sizey + j] + COF);
  317.         rectfi(i, j, i+2, j+2);
  318.     }
  319.     }
  320.  
  321.     if (res == HI) {
  322.     if (eightbit) {
  323.         for (i=0; i<x; i++) for (j=0; j<sizey; j++) {
  324.         color (ebp_maparray[save[i * sizey + j]]);
  325.         pnt2i(i,j);
  326.         }
  327.         for ( i = x, j = 0; j < y; j++) {
  328.         color (ebp_maparray[save[i * sizey + j]]);
  329.         pnt2i(i,j);
  330.         }
  331.     } else {
  332.         for (i=0; i<x; i++) for (j=0; j<sizey; j++) {
  333.         color (save[i * sizey + j] + COF);
  334.         pnt2i(i,j);
  335.         }
  336.         for ( i = x, j = 0; j < y; j++) {
  337.         color (save[i * sizey + j] + COF);
  338.         pnt2i(i,j);
  339.         }
  340.     }
  341.     }
  342.     
  343. }
  344.  
  345. make_new_space()
  346. {
  347.  
  348.     long            mx, my, omx, omy, cx, cy, bx, by, obx, oby;
  349.     float           corner1x, corner1y, corner2x, corner2y;
  350.  
  351.     if (eightbit) {
  352.     drawmode(PUPDRAW);
  353.     mapcolor(1, 255, 255, 255);
  354.     } else
  355.     drawmode(OVERDRAW);
  356.  
  357.     qreset();
  358.  
  359.     omx = mx = getvaluator(MOUSEX) - orx;
  360.     omy = my = getvaluator(MOUSEY) - ory;
  361.  
  362.     obx = bx = cx = mx;
  363.     oby = by = cy = my;
  364.  
  365.     corner1y = (double) my / (double) sizey *(spt - spb) + spb;
  366.     corner1x = (double) mx / (double) sizex *(spr - spl) + spl;
  367.  
  368.     color(0);
  369.     clear();
  370.  
  371.  
  372.     while (!qtest()) {
  373.  
  374.     mx = getvaluator(MOUSEX) - orx;
  375.     my = getvaluator(MOUSEY) - ory;
  376.  
  377.     if (mx != omx && my != omy) {
  378.  
  379.         if (abs(mx - cx) * sizey / sizex > abs(my - cy)) {
  380.         bx = mx;
  381.         by = cy + abs(mx - cx) * sgn(my - cy) * sizey / sizex;
  382.         } else {
  383.         by = my;
  384.         bx = cx + abs(my - cy) * sgn(mx - cx) * sizex / sizey;
  385.         }
  386.  
  387.         color(0);
  388.         recti(cx, cy, obx, oby);
  389.  
  390.         color(1);
  391.         recti(cx, cy, bx, by);
  392.  
  393.         obx = bx;
  394.         oby = by;
  395.         omx = mx;
  396.         omy = my;
  397.     }
  398.     }
  399.  
  400.     if (qtest() != LEFTMOUSE)
  401.     return;
  402.     else
  403.     qread(&val);
  404.  
  405.     if (by != cy && bx != cx) {
  406.     corner2y = (double) by / (double) sizey *(spt - spb) + spb;
  407.     corner2x = (double) bx / (double) sizex *(spr - spl) + spl;
  408.  
  409.     /* if (corner2y != corner1y && corner2x != corner1x) { */
  410.  
  411.     if (corner2y < corner1y) {
  412.         spb = corner2y;
  413.         spt = corner1y;
  414.     } else {
  415.         spb = corner1y;
  416.         spt = corner2y;
  417.     }
  418.  
  419.     if (corner2x < corner1x) {
  420.         spl = corner2x;
  421.         spr = corner1x;
  422.     } else {
  423.         spl = corner1x;
  424.         spr = corner2x;
  425.     }
  426.  
  427.     x = 0;
  428.     y = 0;
  429.     res = LOW;
  430.     }
  431.  
  432.     color(0);
  433.     clear();
  434.  
  435.     drawmode(NORMALDRAW);
  436.  
  437. }
  438.  
  439.  
  440. make_menus() {
  441.  
  442.     menu = defpup("DRAGON%t|reset|save RGB image|save map image|colors|exit");
  443.  
  444.     color_menu = defpup("COLOR MENU%t|shell|fire|stripe|rainbow");
  445.  
  446.     /* color_menu = defpup("COLOR MENU%t|shell|fire|stripe"); */
  447.     
  448. }
  449.  
  450.  
  451. make_colors(i)
  452. int i;
  453. {
  454.     int j;
  455.     int ramps, c, index;
  456.  
  457.     if (eightbit)
  458.     ebp_makemap();
  459.  
  460.     switch (i) {
  461.  
  462.     case 2:
  463.     makerange(0, 255, 0, 255, 0, 0, 0, 0);
  464.     makerange(256, 511, 255, 255, 0, 255, 0, 0);
  465.     makerange(512, 767, 255, 255, 255, 0, 0, 255);
  466.     makerange(768, 1023, 255, 0, 0, 0, 255, 255);
  467.     break;
  468.  
  469.     case 3:
  470.     if (eightbit) {
  471.         for (j = 0; j < 128; j++) {
  472.         ebp_maparray[j * 2] = (short)rgbi(j * 2, 0 ,255);
  473.         ebp_maparray[j * 2 + 1] = (short)rgbi(0, j * 2, 0);
  474.         }
  475.     } else {
  476.         for (j = 0; j < 128; j++) {
  477.         mapcolor(j * 2 + COF, j * 2, 0, 255);
  478.         mapcolor(j * 2 + COF + 1, 0, j * 2, 0);
  479.         }
  480.     }
  481.     makerange(256, 511, 0, 255, 255, 0, 0, 255);
  482.     makerange(512, 767, 255, 0, 0, 255, 255, 255);
  483.     makerange(768, 1023, 0, 255, 255, 255, 255, 255);
  484.     break;
  485.  
  486.     case 4:
  487.     index = 0;
  488.     if (eightbit) {
  489.         for (ramps = 0; ramps < 128; ramps ++) {
  490.         for (c = 0; c < 8; c ++) {
  491.             ebp_maparray[index++] = 
  492.                 (short)rgbi((c&1)*255,((c&2)>>1)*255,
  493.                 ((c&4)>>2)*255);
  494.         }
  495.         }
  496.     } else { 
  497.         for (ramps = 0; ramps < 128; ramps ++) {
  498.         for (c = 0; c < 8; c ++) {
  499.             mapcolor(COF+index++,(c&1)*255,((c&2)>>1)*255,
  500.                 ((c&4)>>2)*255);
  501.         }
  502.         }
  503.     }
  504.     break;
  505.  
  506.     default:            /* changed to geometric progression - dbl */
  507.     /* Make a geometric progression (n2 = n1 * 1.3; n3 = n2 * 1.3 ...) */
  508.     makerange(0, 14, 0, 0, 0, 0, 0, 0);
  509.     makerange(15, 19, 0, 0, 200, 0, 130, 0);
  510.     makerange(20, 25, 0, 0, 150, 0, 160, 0);
  511.     makerange(26, 33, 0, 0, 100, 0, 190, 0);
  512.     makerange(34, 43, 0, 0, 50, 0, 220, 0);
  513.     makerange(44, 56, 50, 0, 0, 0, 250, 0);
  514.     makerange(57, 73, 100, 0, 0, 0, 255, 0);
  515.     makerange(74, 96, 150, 0, 0, 0, 255, 0);
  516.     makerange(97, 125, 200, 0, 0, 0, 255, 0);
  517.     makerange(126, 162, 255, 0, 0, 0, 200, 0);
  518.     makerange(163, 211, 255, 0, 0, 0, 150, 0);
  519.     makerange(212, 275, 255, 0, 0, 0, 100, 0);
  520.     makerange(276, 358, 255, 0, 0, 0, 50, 0);
  521.     makerange(359, 465, 255, 0, 0, 0, 0, 0);
  522.     makerange(466, 605, 170, 0, 85, 0, 0, 0);
  523.     makerange(606, 787, 85, 0, 170, 0, 0, 0);
  524.     makerange(788, 1023, 0, 0, 255, 0, 0, 0);
  525.  
  526.     break;
  527.  
  528.     }
  529.  
  530.     if (!eightbit) {
  531.     overlay(2);
  532.     gconfig();
  533.     drawmode(OVERDRAW);
  534.     mapcolor(1, 255, 255, 255);
  535.     drawmode(NORMALDRAW);
  536.     }
  537. }
  538.  
  539.  
  540. makerange(a,b,r1,r2,g1,g2,b1,b2)
  541. int a,b;
  542. int r1,r2,g1,g2,b1,b2;
  543. {
  544.     int i;
  545.     int j;
  546.     float dr, dg, db;
  547.  
  548.     if (a != b) {
  549.     i = b - a;
  550.     dr = (float)(r2 - r1) / (float)i;
  551.     dg = (float)(g2 - g1) / (float)i;
  552.     db = (float)(b2 - b1) / (float)i;
  553.  
  554.     if (eightbit) {
  555.         for (j = 0; j <= i; j++) {
  556.         ebp_maparray[j + a] = (Colorindex)rgbi(
  557.             (int)(dr * (float) j + r1),
  558.             (int)(dg * (float) j + g1),
  559.             (int)(db * (float) j + b1));
  560.         }
  561.     } else {
  562.         a += COF;
  563.         for (j = 0; j <= i; j++) {
  564.         mapcolor((Colorindex)(j + a),
  565.             (RGBvalue)(dr * (float) j + r1),
  566.             (RGBvalue)(dg * (float) j + g1),
  567.             (RGBvalue)(db * (float) j + b1));
  568.         }
  569.     }
  570.  
  571.     } else {
  572.     if (eightbit) {
  573.         ebp_maparray[a] = (Colorindex)(rgbi((int)r1, (int)g1, (int)b1));
  574.     } else {
  575.         mapcolor((Colorindex)(a + COF),
  576.         (RGBvalue)r1, (RGBvalue)g1, (RGBvalue)b1);
  577.     }
  578.     }
  579. }
  580.  
  581.  
  582. select_point() {
  583.  
  584.     int mx, my, omx, omy;
  585.  
  586.     if (eightbit) {
  587.     drawmode(PUPDRAW);
  588.     mapcolor(1,255,255,255);
  589.     } else
  590.     drawmode(OVERDRAW);
  591.  
  592.     while (!qtest()) {
  593.  
  594.     mx = getvaluator(MOUSEX)-orx;
  595.     my = getvaluator(MOUSEY)-ory;
  596.  
  597.     if (mx!=omx || my!=omy) {
  598.  
  599.         color(0); 
  600.         move2i(0,omy); draw2i(sizex,omy);
  601.         move2i(omx,0); draw2i(omx,sizey);
  602.  
  603.         color(1);
  604.         move2i(0, my); draw2i(sizex, my);
  605.         move2i(mx, 0); draw2i(mx, sizey);
  606.  
  607.         omx=mx; omy=my;
  608.     }
  609.     }
  610.  
  611.     color(0);
  612.     clear();
  613.  
  614.     drawmode(NORMALDRAW);
  615.  
  616.     a=((double)mx/(double)sizex) * (spr-spl) + spl;
  617.     b=((double)my/(double)sizey) * (spt-spb) + spb;
  618. }
  619.  
  620.  
  621. do_map()
  622. {
  623.     register int    colur;
  624.     register double q, r, nq, nr, q2, r2;
  625.  
  626.     for (; x < sizex && res == LOW && !qtest(); x += 3) {
  627.     for (; y < sizey; y += 3) {
  628.  
  629.         q = inq;
  630.         r = inr;
  631.         colur = 0;
  632.         c = ((double) x / (double) sizex) * (spr - spl) + spl;
  633.         d = ((double) y / (double) sizey) * (spt - spb) + spb;
  634.  
  635.         do {
  636.  
  637.         q2 = q * q;
  638.         r2 = r * r;
  639.         nq = q2 - r2 - c;
  640.         nr = 2 * q * r - d;
  641.  
  642.         q = nq;
  643.         r = nr;
  644.  
  645.         } while (q2 + r2 < 100.0 && ++colur < 256);
  646.  
  647.         if (colur == 256)
  648.         colur = 0;
  649.  
  650.         save[x * sizey + y] = colur;
  651.  
  652.         if (eightbit)
  653.         color(ebp_maparray[colur]);
  654.         else
  655.         color(colur + COF);
  656.         rectf(x, y, x + 2, y + 2);
  657.     }
  658.     y = 0;
  659.     }
  660.  
  661.     if (!qtest() && x >= sizex && res == LOW) {
  662.     res = HI;
  663.     x = 0;
  664.     y = 0;
  665.     }
  666.     for (; x < sizex && res == HI && !qtest(); x++) {
  667.     for (; y < sizey; y++) {
  668.         register int    colur;
  669.         register double q, r, nq, nr, q2, r2;
  670.  
  671.         q = inq;
  672.         r = inr;
  673.         colur = 0;
  674.         c = ((double) x / (double) sizex) * (spr - spl) + spl;
  675.         d = ((double) y / (double) sizey) * (spt - spb) + spb;
  676.  
  677.         do {
  678.  
  679.         q2 = q * q;
  680.         r2 = r * r;
  681.         nq = q2 - r2 - c;
  682.         nr = 2 * q * r - d;
  683.  
  684.         q = nq;
  685.         r = nr;
  686.  
  687.         } while (q2 + r2 < 100.0 && ++colur < 1024);
  688.  
  689.  
  690.         if (colur == 1024)
  691.         colur = 0;
  692.         save[x * sizey + y] = colur;
  693.  
  694.         if (eightbit)
  695.         color(ebp_maparray[colur]);
  696.         else
  697.         color(colur + COF);
  698.  
  699.         pnt2i(x, y);
  700.     }
  701.     y = 0;
  702.     }
  703. }
  704.  
  705.  
  706. do_dragon()
  707. {
  708.  
  709.     register int    colur;
  710.     register double q, r, nq, nr, q2, r2;
  711.  
  712.     for (; x < sizex && res == LOW && !qtest(); x += 3) {
  713.     for (; y < sizey; y += 3) {
  714.  
  715.         colur = 0;
  716.         q = ((double) x / (double) sizex) * (spr - spl) + spl;
  717.         r = ((double) y / (double) sizey) * (spt - spb) + spb;
  718.         c = a;
  719.         d = b;
  720.  
  721.         do {
  722.  
  723.         q2 = q * q;
  724.         r2 = r * r;
  725.         nq = q2 - r2 - c;
  726.         nr = 2 * q * r - d;
  727.  
  728.         q = nq;
  729.         r = nr;
  730.  
  731.         } while (q2 + r2 < 100.0 && ++colur < 256);
  732.  
  733.         if (colur == 256)
  734.         colur = 0;
  735.         save[x * sizey + y] = colur;
  736.  
  737.         if (eightbit)
  738.         color(ebp_maparray[colur]);
  739.         else
  740.         color(colur + COF);
  741.  
  742.         rectf(x, y, x + 2, y + 2);
  743.     }
  744.     y = 0;
  745.     }
  746.  
  747.     if (!qtest() && x >= sizex && res == LOW) {
  748.     res = HI;
  749.     x = 0;
  750.     y = 0;
  751.     }
  752.     for (; x < sizex && res == HI && !qtest(); x++) {
  753.     for (; y < sizey; y++) {
  754.         register int    colur;
  755.         register double q, r, nq, nr, q2, r2;
  756.  
  757.         colur = 0;
  758.         q = ((double) x / (double) sizex) * (spr - spl) + spl;
  759.         r = ((double) y / (double) sizey) * (spt - spb) + spb;
  760.  
  761.         do {
  762.  
  763.         q2 = q * q;
  764.         r2 = r * r;
  765.         nq = q2 - r2 - c;
  766.         nr = 2 * q * r - d;
  767.  
  768.         q = nq;
  769.         r = nr;
  770.  
  771.         } while (q2 + r2 < 100.0 && ++colur < 1024);
  772.  
  773.         if (colur == 1024)
  774.         colur = 0;
  775.         save[x * sizey + y] = colur;
  776.  
  777.         if (eightbit)
  778.         color(ebp_maparray[colur]);
  779.         else
  780.         color(colur + COF);
  781.  
  782.         pnt2i(x, y);
  783.     }
  784.     y = 0;
  785.     }
  786.  
  787. }
  788.  
  789.  
  790.  
  791. savescreen(name, sizex, sizey)
  792. char *name;
  793. int sizex, sizey;
  794. {
  795.     IMAGE *image;
  796.     long i, y;
  797.     Colorindex buff[XMAXSCREEN];
  798.  
  799.     if((image = iopen(name, "w", RLE(2), 2, sizex, sizey, 0)) == NULL){
  800.     fprintf(stderr,"dragon: couldn't open file '%s'\n",name);
  801.     return (-1);
  802.     }
  803.     isetname(image,name);
  804.     isetcolormap(image, CM_SCREEN);
  805.  
  806.     pushviewport();
  807.     viewport(0, XMAX, 0, YMAX);
  808.     pushmatrix();
  809.     ortho2(-0.5, (float)XMAX+.5, -0.5, (float)YMAX+.5);
  810.  
  811.     for(y=0; y<sizey; y++) {
  812.     cmov2i((int)0, (int)y);
  813.     i = readpixels((short)sizex, buff);
  814.     putrow(image,buff,y,0);
  815.     }
  816.     popmatrix();
  817.  
  818.     popviewport();
  819.     reshapeviewport();
  820.  
  821.     iclose(image);
  822.     return 0;
  823. }
  824.  
  825.  
  826. /* this thing reads the screen colormapped image and writes
  827.    an RGB image file based on the contents of the colormap */
  828.  
  829. saveRGBscreen(name, sizex, sizey)
  830. char *name;
  831. int sizex, sizey;
  832. {
  833.     IMAGE *image;
  834.     long i, j, y;
  835.     Colorindex buff[XMAXSCREEN];
  836.     Colorindex rbuff[XMAXSCREEN];
  837.     Colorindex gbuff[XMAXSCREEN];
  838.     Colorindex bbuff[XMAXSCREEN];
  839.  
  840.     Colorindex c[1024][3];
  841.  
  842.     if (eightbit) {
  843.     for (i = 0; i < 256; i++) {
  844.         getmcolor(i, &c[i][0], &c[i][1], &c[i][2]);
  845.     }
  846.     } else {
  847.     for (i=0; i<1024; i++) {
  848.         getmcolor(i+COF, &c[i][0], &c[i][1], &c[i][2]);
  849.     }
  850.     }
  851.  
  852.     if((image = iopen(name, "w", RLE(1), 3, sizex, sizey, 3)) == NULL){
  853.     fprintf(stderr,"dragon: couldn't open file '%s'\n",name);
  854.     return (-1);
  855.     }
  856.     isetname(image,name);
  857.     isetcolormap(image, CM_NORMAL);
  858.  
  859.     pushviewport();
  860.     viewport(0, XMAX, 0, YMAX);
  861.     pushmatrix();
  862.     ortho2(-0.5, (float)XMAX+.5, -0.5, (float)XMAX+.5);
  863.  
  864.     for(y=0; y<sizey; y++) {
  865.     cmov2i((int)0, (int)y);
  866.     i = readpixels((Colorindex)sizex, buff);
  867.  
  868.     for (j=0; j<i; j++) {
  869.         if (eightbit) {
  870.         rbuff[j] = c[buff[j]&0xFF][0];
  871.         gbuff[j] = c[buff[j]&0xFF][1];
  872.         bbuff[j] = c[buff[j]&0xFF][2];
  873.         } else {
  874.         
  875.         rbuff[j] = c[buff[j]&0xFFFFFF-COF][0];
  876.         gbuff[j] = c[buff[j]&0xFFFFFF-COF][1];
  877.         bbuff[j] = c[buff[j]&0xFFFFFF-COF][2];
  878.         }
  879.     }
  880.     putrow(image, rbuff, y, 0);
  881.     putrow(image, gbuff, y, 1);
  882.     putrow(image, bbuff, y, 2);
  883.     }
  884.     popmatrix();
  885.  
  886.     popviewport();
  887.     reshapeviewport();
  888.  
  889.     iclose(image);
  890.     return 0;
  891. }
  892.  
  893.  
  894. ebp_makemap()
  895. {
  896.     int r, g, b;
  897.     Colorindex i;
  898.  
  899.     static short green[8] = {0, 36, 72, 109, 145, 182, 218, 255};
  900.     static short red_blue[5] = {0, 63, 127, 191, 255};
  901.  
  902.     i = EBP_COF;
  903.     for (b = 0; b < 5; b++) {
  904.     for (r = 0; r < 5; r++) {
  905.         for (g = 0; g < 8; g++) {
  906.         mapcolor(i++, red_blue[r], green[g], red_blue[b]);
  907.         }
  908.     }
  909.     }
  910. }
  911.  
  912. static short SavedMap[1024][3];
  913.  
  914. save_colormap()
  915. {
  916.     int i;
  917.  
  918.     if (eightbit)
  919.     {
  920.         for (i = 0; i < 256; i++)
  921.         {
  922.             getmcolor(i, SavedMap[i]+0, SavedMap[i]+1, SavedMap[i]+2);
  923.         }
  924.     }
  925.     else
  926.     {
  927.         for (i = 0; i < 1024; i++)
  928.         {
  929.             getmcolor(i+COF, SavedMap[i]+0, SavedMap[i]+1, SavedMap[i]+2);
  930.         }
  931.     }
  932. }
  933.  
  934. restore_colormap()
  935. {
  936.     int i;
  937.  
  938.     if (eightbit)
  939.     {
  940.         for (i = 0; i < 256; i++)
  941.         {
  942.             mapcolor(i, SavedMap[i][0], SavedMap[i][1], SavedMap[i][2]);
  943.         }
  944.     }
  945.     else
  946.     {
  947.         for (i = 0; i < 1024; i++)
  948.         {
  949.             mapcolor(i+COF, SavedMap[i][0], SavedMap[i][1], SavedMap[i][2]);
  950.         }
  951.     }
  952. }
  953.